package com.techysky.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.techysky.annoation.Operation;
import com.techysky.entity.CommentTags;
import com.techysky.service.CommentTagsService;
import com.techysky.util.ExportUtil;

@Controller
@RequestMapping("/file")
public class FileController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private transient CommentTagsService commentTagsService;
	
	
	@RequestMapping("/{format}/export")
	@Operation(value="导出操作",type="2")
	public ResponseEntity<byte[]> export(@PathVariable("format") String format,HttpServletRequest request) throws IOException{
		List<CommentTags> commentTags=this.commentTagsService.findByProject(request.getParameter("projectId")!=null?request.getParameter("projectId").toString():"");
	    return ExportUtil.getData(format, (ByteArrayOutputStream)getExcel(commentTags),"访问记录");
	}
	
	
	/***
	 * 创建表头
	 * @param workbook
	 * @param sheet
	 */
	private void createTitle(HSSFWorkbook workbook, HSSFSheet sheet)
	{
		HSSFRow row = sheet.createRow(0);
		//设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度
		sheet.setColumnWidth(2, 12*256);
		sheet.setColumnWidth(3, 17*256);
		
		//设置为居中加粗
		HSSFCellStyle style = workbook.createCellStyle();
		HSSFFont font = workbook.createFont();
		font.setBold(true);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setFont(font);
		
		HSSFCell cell;
		cell = row.createCell(0);
		cell.setCellValue("数据模块编码");
		cell.setCellStyle(style);
		
		cell = row.createCell(1);
		cell.setCellValue("标题");
		cell.setCellStyle(style);
		
		cell = row.createCell(2);
		cell.setCellValue("内容");
		cell.setCellStyle(style);
		
		cell = row.createCell(3);
		cell.setCellValue("可见方式");
		cell.setCellStyle(style);
		
		cell = row.createCell(4);
		cell.setCellValue("所属用户");
		cell.setCellStyle(style);
		
		cell = row.createCell(4);
		cell.setCellValue("日期");
		cell.setCellStyle(style);
	}
	
	/***
	 * 获取excel数据
	 * @return 返回文件名称及excel文件的URL
	 * @throws IOException
	 */
	
	public OutputStream getExcel(List<CommentTags> commentTags) throws IOException
	{
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("注释信息表");
		createTitle(workbook, sheet);
		OutputStream os=new ByteArrayOutputStream();
		
		
		//设置日期格式
		HSSFCellStyle style=workbook.createCellStyle();
		style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
		
		//新增数据行，并且设置单元格数据
		int rowNum = 1;
		for (CommentTags commentTag:commentTags) {
			
			HSSFRow row = sheet.createRow(rowNum);
			row.createCell(0).setCellValue(commentTag.getCode());
			row.createCell(1).setCellValue(commentTag.getTitle());
			row.createCell(2).setCellValue(commentTag.getContent());
			row.createCell(3).setCellValue(!ObjectUtils.isEmpty(commentTag.getVisibility())?("0".equals(commentTag.getVisibility())?"共享":"私有"):"");
			row.createCell(4).setCellValue(commentTag.getPublishDate());
			rowNum++;
		}
		
		//拼装blobName
		workbook.write(os);
		return os;
	}

}
